import { ref, reactive } from 'vue'

export interface ValidationRule {
  required?: boolean
  pattern?: RegExp
  minLength?: number
  maxLength?: number
  validator?: (value: any) => boolean | string
  message: string
}

export interface FormField {
  value: any
  rules: ValidationRule[]
  error?: string
  touched: boolean
}

export interface FormState {
  [key: string]: FormField
}

/**
 * 表单验证 composable
 */
export function useFormValidation(initialFields: Record<string, any>) {
  const formState = reactive<FormState>({})
  
  // 初始化表单字段
  Object.keys(initialFields).forEach(key => {
    formState[key] = {
      value: initialFields[key].value || '',
      rules: initialFields[key].rules || [],
      touched: false
    }
  })

  // 验证单个字段
  const validateField = (fieldName: string): boolean => {
    const field = formState[fieldName]
    field.touched = true
    
    for (const rule of field.rules) {
      // 必填验证
      if (rule.required && (!field.value || String(field.value).trim() === '')) {
        field.error = rule.message
        return false
      }
      
      // 正则表达式验证
      if (rule.pattern && field.value && !rule.pattern.test(String(field.value))) {
        field.error = rule.message
        return false
      }
      
      // 最小长度验证
      if (rule.minLength && field.value && String(field.value).length < rule.minLength) {
        field.error = rule.message
        return false
      }
      
      // 最大长度验证
      if (rule.maxLength && field.value && String(field.value).length > rule.maxLength) {
        field.error = rule.message
        return false
      }
      
      // 自定义验证器
      if (rule.validator) {
        const result = rule.validator(field.value)
        if (result === false || typeof result === 'string') {
          field.error = typeof result === 'string' ? result : rule.message
          return false
        }
      }
    }
    
    field.error = undefined
    return true
  }

  // 验证整个表单
  const validateForm = (): boolean => {
    let isValid = true
    Object.keys(formState).forEach(fieldName => {
      if (!validateField(fieldName)) {
        isValid = false
      }
    })
    return isValid
  }

  // 重置表单
  const resetForm = () => {
    Object.keys(formState).forEach(fieldName => {
      const field = formState[fieldName]
      field.value = initialFields[fieldName].value || ''
      field.error = undefined
      field.touched = false
    })
  }

  // 清除错误
  const clearErrors = () => {
    Object.keys(formState).forEach(fieldName => {
      formState[fieldName].error = undefined
    })
  }

  // 设置字段值
  const setFieldValue = (fieldName: string, value: any) => {
    if (formState[fieldName]) {
      formState[fieldName].value = value
    }
  }

  // 获取表单数据
  const getFormData = (): Record<string, any> => {
    const formData: Record<string, any> = {}
    Object.keys(formState).forEach(fieldName => {
      formData[fieldName] = formState[fieldName].value
    })
    return formData
  }

  return {
    formState,
    validateField,
    validateForm,
    resetForm,
    clearErrors,
    setFieldValue,
    getFormData
  }
}